/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery.order.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.LinkedList;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;

import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.BTN_HAS;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-03 下午1:37<br/>
 * <p>
 * 更多操作按钮
 * </p>
 */
public class MoreOperatePopupView extends BasePopupWindow {
    private String buttonList;
    private LinearLayout llContainer;
    private View.OnClickListener orderOperatorCallback;

    public MoreOperatePopupView(Context context, View.OnClickListener listener, String buttonList) {
        super(context);
        this.buttonList = buttonList;
        this.orderOperatorCallback = listener;
        initView(context);
        setDefaultAttr();
    }

    private void setDefaultAttr() {
        setOutsideTouchable(true);
        setAnimationStyle(R.style.popwin_anim_style);
        setFocusable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.popup_more_orderfunc, null);
        view.findViewById(R.id.sheet_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        llContainer = (LinearLayout) view.findViewById(R.id.ll_operation_container);
        refreshChildView();
        setContentView(view);
    }

    public Context getContext() {
        return llContainer.getContext();
    }

    private void refreshChildView() {
        // 查看拆分 查看改价备注 订单跟踪
        LinkedList<WrapAttr> showViewList = new LinkedList<>();
        String[] split = buttonList.split("");
        if (split[3].equals(BTN_HAS)) {
            showViewList.add(new WrapAttr(R.id.btn_order_track, R.string.order_track));
        }
        if (split[1].equals(BTN_HAS)) {
            showViewList.add(new WrapAttr(R.id.btn_order_split, R.string.view_order_split));
        }
        if (split[2].equals(BTN_HAS)) {
            showViewList.add(new WrapAttr(R.id.btn_order_change_price_note, R.string.view_change_price_note));
        }
        // may be add ...
        if (showViewList.isEmpty()) {
            return;
        }
        LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtils.dipToPx(getContext(), 42));
        if (showViewList.size() == 1) {
            WrapAttr wrapAttr = showViewList.get(0);
            Button orderSplitView = createChildView(wrapAttr.viewId, R.color.title_bg, wrapAttr.strId, R.drawable.selector_actionsheet_cancel);
            llContainer.addView(orderSplitView, childLp);
        } else {
            WrapAttr firstAttr = showViewList.removeFirst();
            Button firstBtnView = createChildView(firstAttr.viewId, R.color.title_bg, firstAttr.strId, R.drawable.selector_actionsheet_top);
            firstBtnView.setLayoutParams(childLp);
            llContainer.addView(firstBtnView);
            for (int i = 0; i < showViewList.size() - 1; i++) {
                View view = createDivider();
                llContainer.addView(view);
                WrapAttr value = showViewList.get(i);
                Button orderSplitView = createChildView(value.viewId, R.color.title_bg, value.strId, R.drawable.selector_actionsheet_middle);
                orderSplitView.setLayoutParams(childLp);
                llContainer.addView(orderSplitView);
            }
            View view = createDivider();
            llContainer.addView(view);
            WrapAttr lastAttr = showViewList.removeLast();
            Button lastAttrBtnView = createChildView(lastAttr.viewId, R.color.title_bg, lastAttr.strId, R.drawable.selector_actionsheet_bottom);
            lastAttrBtnView.setLayoutParams(childLp);
            llContainer.addView(lastAttrBtnView);
        }
    }

    private View createDivider() {
        View view = new View(getContext());
        LinearLayout.LayoutParams viewLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
        view.setBackgroundColor(getContext().getResources().getColor(R.color.eeeeee));
        view.setLayoutParams(viewLp);
        return view;
    }

    private Button createChildView(int id, int textColor, int text, int backgoundDrawable) {
        final Button button = new Button(llContainer.getContext(), null, R.attr.borderlessButtonStyle);
        button.setId(id);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                orderOperatorCallback.onClick(v);
            }
        });
        setButtonStyle(button, textColor, 14, text, backgoundDrawable);
        return button;
    }

    private void setButtonStyle(Button textView, int textColor, int textSize, int text, int backgroud) {
        textView.setTextSize(textSize);
        textView.setTextColor(getContext().getResources().getColor(textColor));
        textView.setText(text);
        textView.setBackgroundResource(backgroud);
    }

    private class WrapAttr {
        public int viewId;
        public int strId;

        public WrapAttr(int viewId, int strId) {
            this.viewId = viewId;
            this.strId = strId;
        }
    }
}
